<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item :label="$t('deviceDetail.task.name')" prop="name">
        <el-input
          v-model="queryParams.name"
          :placeholder="$t('deviceDetail.task.namePlaceholder')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('common.search')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('common.reset')}}</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="$t('deviceDetail.task.name')" align="center" prop="name" />
      <el-table-column :label="$t('deviceDetail.task.type')" align="center" prop="dataType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_data_type" :value="scope.row.dataType"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('deviceDetail.task.slave')" align="center" prop="slaveStationAddr" />
      <el-table-column :label="$t('deviceDetail.task.funcCode')" align="center" prop="funcCode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_func_code" :value="scope.row.funcCode"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('deviceDetail.task.dataAddress')" align="center" prop="dataAddr" />
      <el-table-column :label="$t('deviceDetail.task.timer')" align="center" prop="timer" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_enabled" :value="scope.row.timer"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('deviceDetail.task.frequency')" align="center" prop="times" />
      <el-table-column :label="$t('deviceDetail.task.dataChange')" align="center" prop="dataChange" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_enabled" :value="scope.row.dataChange"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('deviceDetail.task.changeRange')" align="center" prop="changeValue" />
      <el-table-column :label="$t('deviceDetail.task.Threshold')" align="center" prop="threshold" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_enabled" :value="scope.row.threshold"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('deviceDetail.task.condition')" align="center" prop="condition" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_condition" :value="scope.row.condition"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('deviceDetail.task.lessThanThreshold')" align="center" prop="thrMin" />
      <el-table-column :label="$t('deviceDetail.task.greaterThanThreshold')" align="center" prop="thrMax" />
      <el-table-column :label="$t('deviceDetail.task.delay')" align="center" prop="delay" />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ems:task:edit']"
          >修改</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备标识任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" :inline="true">
        <el-form-item label="数据类型" prop="dataType">
          <el-select  v-model="form.dataType" placeholder="请选择数据类型" class="three_input three_input_mragin w200">
            <el-option label="位（bit）" :value="0">位（bit）</el-option>
            <el-option label="大端 16 位无符号" :value="3">大端 16 位无符号</el-option>
            <el-option label="大端 16 位有符号" :value="4">大端 16 位有符号</el-option>
            <el-option label="大端 32 位无符号" :value="5">大端 32 位无符号</el-option>
            <el-option label="大端 32 位有符号" :value="6">大端 32 位有符号</el-option>
            <el-option label="大端 32 位浮点数" :value="7">大端 32 位浮点数</el-option>
            <el-option label="大端 64 位无符号" :value="8">大端 64 位无符号</el-option>
            <el-option label="大端 64 位有符号" :value="9">大端 64 位有符号</el-option>
            <el-option label="大端 64 位浮点数" :value="10">大端 64 位浮点数</el-option>
            <el-option label="小端 16 位无符号" :value="11">小端 16 位无符号</el-option>
            <el-option label="小端 16 位有符号" :value="12">小端 16 位有符号</el-option>
            <el-option label="小端 32 位无符号" :value="13">小端 32 位无符号</el-option>
            <el-option label="小端 32 位有符号" :value="14">小端 32 位有符号</el-option>
            <el-option label="小端 32 位浮点数" :value="15">小端 32 位浮点数</el-option>
            <el-option label="小端 64 位无符号" :value="16">小端 64 位无符号</el-option>
            <el-option label="小端 64 位有符号" :value="17">小端 64 位有符号</el-option>
            <el-option label="小端 64 位浮点数" :value="18">小端 64 位浮点数</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="功能码" prop="funcCode">
          <el-select v-model="form.funcCode" placeholder="请选择数据类型" class="three_input three_input_mragin w200">
            <el-option label="01H,05H,15H 功能码，读写线圈" :value="0">01H,05H,15H 功能码，读写线圈</el-option>
            <el-option label="02H 功能码，读输入离散量" :value="1">02H 功能码，读输入离散量</el-option>
            <el-option label="04H 功能码，读输入寄存器" :value="2">04H 功能码，读输入寄存器</el-option>
            <el-option label="03H,06H,10H 功能码，读写保存寄存器" :value="3">03H,06H,10H 功能码，读写保存寄存器</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="从站地址" prop="slaveStationAddr">
          <el-input  class="w200" v-model="form.slaveStationAddr" placeholder="请输入从站地址" />
        </el-form-item>
        <el-form-item label="寄存器地址" prop="dataAddr">
          <el-input  class="w200" v-model="form.dataAddr" placeholder="请输入寄存器地址" />
        </el-form-item>
        <el-form-item label="采样周期" prop="period" v-if="form.transmission==0">
          <el-input  class="w200" v-model="form.period" placeholder="请输入采样周期" />  ms
        </el-form-item>
        <el-form-item label="应答时间" prop="respTime" v-if="form.transmission==0">
          <div class="w400">
            <el-input  class="w200" v-model="form.respTime" placeholder="请输入应答时间 ms" />  ms
          </div>
        </el-form-item>
        <el-divider content-position="center" v-if="control==0">数据上传方式</el-divider>
        <el-form-item label="定时上传" prop="timer" v-if="form.transmission==0">
          <el-switch  class="w200" style="display: block" v-model="form.timer" active-color="#13ce66"  active-text="启用" :active-value="1" inactive-color="#ff4949" inactive-text="禁用" :inactive-value="0" />
        </el-form-item>
        <el-form-item label="上传频率" prop="times" v-if="form.transmission==0">
          <div class="w400">
            <el-input-number  class="w200" :min="0" v-model="form.times" placeholder="请输入定时上传频率" :disabled="form.timer===0"/> s
          </div>
        </el-form-item>
        <el-form-item label="数值变化" prop="dataChange" v-if="form.transmission==0">
          <el-switch  class="w200" style="display: block" v-model="form.dataChange" active-color="#13ce66"  active-text="启用" :active-value="1" inactive-color="#ff4949" inactive-text="禁用" :inactive-value="0" />
        </el-form-item>
        <el-form-item label="变化范围" prop="changeValue" v-if="form.transmission==0">
          <div class="w400">
            <el-input-number  class="w200" v-model="form.changeValue" placeholder="请输入数据变化范围" :disabled="form.dataChange===0"/>
          </div>
        </el-form-item>
        <el-form-item label="阈值触发" prop="threshold" v-if="form.transmission==0">
          <el-switch  class="w130" style="display: block" v-model="form.threshold" active-color="#13ce66"  active-text="启用" :active-value="1" inactive-color="#ff4949" inactive-text="禁用" :inactive-value="0" />
        </el-form-item>
        <el-form-item label="表达式" prop="condition">
          <el-select v-model="form.condition" placeholder="请选择数据类型" class="three_input three_input_mragin w130" :disabled="form.threshold===0">
            <el-option label="大于" :value="1">大于</el-option>
            <el-option label="小于" :value="3">小于</el-option>
            <el-option label="区间" :value="5">区间</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="小于" prop="thrMin" v-if="form.transmission==0">
          <el-input  class="w130" v-model="form.thrMin" placeholder="请输入小于阈值" :disabled="form.threshold===0 || form.condition===1"/>
        </el-form-item>
        <el-form-item label="大于" prop="thrMax" v-if="form.transmission==0">
          <el-input  class="w130" v-model="form.thrMax" placeholder="请输入大于阈值" :disabled="form.threshold===0 || form.condition===3"/>
        </el-form-item>
        <el-form-item label="持续时间" prop="thrMax" v-if="form.transmission==0">
          <el-input  class="w130" v-model="form.delay" placeholder="请输入持续时间" :disabled="form.threshold===0"/> ms
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDeviceSensorTask, getTask, delTask, addTask, updateTask } from "@/api/data/deviceSensorTask";

export default {
  props:{
    deviceId:null
  },
  name: "Task",
  dicts: ['task_func_code', 'task_enabled', 'task_data_type','task_condition'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备标识任务表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceId: null,
        sensorType:0
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceId: [
          { required: true, message: "设备Id不能为空", trigger: "blur" }
        ],
        sensorId: [
          { required: true, message: "串口标识Id不能为空", trigger: "blur" }
        ],
        transmission: [
          { required: true, message: "透传不能为空", trigger: "blur" }
        ],
        dataType: [
          { required: true, message: "数据类型不能为空", trigger: "change" }
        ],
        start: [
          { required: true, message: "起始位不能为空", trigger: "blur" }
        ],
        period: [
          { required: true, message: "采样周期不能为空", trigger: "blur" }
        ],
        respTime: [
          { required: true, message: "应答时间 ms不能为空", trigger: "blur" }
        ],
        slaveStationAddr: [
          { required: true, message: "从站地址不能为空", trigger: "blur" }
        ],
        funcCode: [
          { required: true, message: "功能码不能为空", trigger: "blur" }
        ],
        dataAddr: [
          { required: true, message: "寄存器地址不能为空", trigger: "blur" }
        ],
        timer: [
          { required: true, message: "定时上传开启不能为空", trigger: "blur" }
        ],
        times: [
          { required: true, message: "定时上传频率不能为空", trigger: "blur" }
        ],
        dataChange: [
          { required: true, message: "数值变化开启不能为空", trigger: "blur" }
        ],
        changeValue: [
          { required: true, message: "数据变化范围不能为空", trigger: "blur" }
        ],
        threshold: [
          { required: true, message: "阈值触发开启不能为空", trigger: "blur" }
        ],
        thrMin: [
          { required: true, message: "小于阈值不能为空", trigger: "blur" }
        ],
        thrMax: [
          { required: true, message: "大于阈值不能为空", trigger: "blur" }
        ],
        delay: [
          { required: true, message: "持续时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.queryParams.deviceId = this.deviceId
    this.getList();
  },
  methods: {
    /** 查询设备标识任务列表 */
    getList() {
      this.loading = true;
      listDeviceSensorTask(this.queryParams).then(response => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deviceId: null,
        sensorId: null,
        transmission: null,
        dataType: null,
        start: null,
        period: null,
        respTime: null,
        slaveStationAddr: null,
        funcCode: null,
        dataAddr: null,
        timer: null,
        times: null,
        dataChange: null,
        changeValue: null,
        threshold: 0,
        thrMin: null,
        thrMax: null,
        delay: null,
        condition: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备标识任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTask(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备标识任务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除设备标识任务编号为"' + ids + '"的数据项？').then(function() {
        return delTask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ems/task/export', {
        ...this.queryParams
      }, `task_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
